package com.bruce.learnrecylerview.app.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.MainThread;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ScrollView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.bruce.learnrecycler.kotlin.action.ActionDelegate;
import com.bruce.learnrecycler.kotlin.store.MainStore;
import com.bruce.learnrecycler.kotlin.ui.activity.NextActivity;
import com.bruce.learnrecycler.kotlin.ui.adapter.MainAdapter;
import com.bruce.learnrecycler.kotlin.ui.listitem.Item;
import com.bruce.learnrecylerview.app.R;
import com.bruce.learnrecylerview.app.action.MainActions;
import com.bruce.learnrecylerview.app.dagger.DaggerComponent;
import com.bruce.learnrecylerview.app.ui.base.ItemTouchBuilder;
import com.github.bluzwang.fluxsupport.Response;
import com.github.bluzwang.fluxsupportkotlin.BaseFluxActivity;
import com.github.bluzwang.fluxsupportkotlin.Store;
import com.github.bluzwong.kotlin_x_weapon.EndlessAdapterWrapper;
import com.github.bluzwong.kotlin_x_weapon.RecyclerHelper;
import com.github.bluzwong.kotlin_x_weapon.SwipeBackActivityHelper;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import jet.runtime.typeinfo.JetValueParameter;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Deprecated
public class MainActivity extends BaseFluxActivity {
    int hashCode = hashCode();
    @Inject
    MainAdapter adapter;
    @Inject
    MainStore mainStore;
    @Inject
    ActionDelegate delegate;
    @InjectView(R.id.recyclerViewMain)
    RecyclerView recyclerViewMain;
    EndlessAdapterWrapper adapterWrapper = null;
    SwipeBackActivityHelper swipeBackHelper = new SwipeBackActivityHelper(this);
    @Override
    protected int provideContentId() {
        return R.layout.activity_main;
    }

    @Nullable
    @Override
    protected Store provideStore() {return mainStore;}


    @Override
    protected void init() {
        ButterKnife.inject(this);
        DaggerComponent.builder().build().inject(this);
        swipeBackHelper.initSwipeBack();
        setRecyclerView();
    }

    @Override
    public void finish() {
        super.finish();
        swipeBackHelper.onSwipeFinish();
    }

    @Override
    protected void onRestoreView() {

    }
    List<Item> list = new ArrayList<>();
    @Override
    protected void onNewView() {
//        delegate.requestByLineNameStationName(hashCode, "89", "田林路桂林路", true, 0);
//        delegate.requestByLineNameStationName(hashCode, "89", "田林路桂林路", true, 0);
//        delegate.requestByLineNameStationName(hashCode, "89", "田林路桂林路", true, 0);

        for (int i = 0; i < 20; i++) {
            list.add(new Item("ccf", R.layout.base_item, "time", R.mipmap.ic_launcher));
        }

    }
    @Override public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();
        adapter.setItems(list);

        recyclerViewMain.scheduleLayoutAnimation();
        recyclerViewMain.setAdapter(adapterWrapper);
    }


    @Override
    public void onResponse(@NotNull String type, @NotNull Map<String, ? extends Object> dataMap) {
        if (dataMap.containsKey(MainActions.KEY_DATAS)) {
            adapter.setItems((List<Item>) dataMap.get(MainActions.KEY_DATAS));
        }
        switch (type) {
            case MainActions.UI_ADD_ONE_ITEM:
                int insertIndex = (Integer) dataMap.get(MainActions.KEY_ITEM_INDEX);
                //                    adapterWrapper?.notifyItemInserted(insertIndex)
                adapterWrapper.notifyDataSetChanged();
                adapterWrapper.loadFinish();
                break;

            case MainActions.UI_REMOVE_ONE_ITEM:
                adapterWrapper.notifyItemRemoved((Integer) dataMap.get(MainActions.KEY_ITEM_INDEX));
                adapterWrapper.reset();
                break;

            case MainActions.UI_SWAP_ONE_ITEM:
                int from = (Integer) dataMap.get(MainActions.KEY_ITEM_INDEX);
                int to = (Integer) dataMap.get(MainActions.KEY_ITEM_INDEX2);
                adapterWrapper.notifyItemMoved(from, to);
                break;
        }
    }

    private void setRecyclerView() {
        adapterWrapper = new EndlessAdapterWrapper(recyclerViewMain, adapter, LayoutInflater.from(this), R.layout.twoball_loading);
        adapterWrapper.setOnLoadListener(6, 200, () -> {
            delegate.requestByLineNameStationName(hashCode, "89", "田林路桂林路", true, -1);
            return null;
        });

        recyclerViewMain.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        ItemTouchBuilder
                .builder()
                .dragDown()
                .dragUp()
                .swipeStart()
                .setSwiped((viewHolder, direction) -> mainStore.requestRemoveItem(viewHolder.getAdapterPosition()))
                .setMove((recyclerView, viewHolder1, target) -> {
                    mainStore.requestSwapItem(viewHolder1.getAdapterPosition(), target.getAdapterPosition());
                    return true;
                })
                .attachToRecyclerView(recyclerViewMain);
        RecyclerHelper.INSTANCE$.setOnRecyclerItemClickListener(recyclerViewMain, viewHolder -> {
            startActivity(new Intent(this, NextActivity.class));
            return null;
        });
    }

}
